package view;

import java.util.Vector;

import org.jfree.chart.ChartPanel;

import ui.components.BarChart;
import ui.components.Chart;
import ui.components.PieChart;

import controller.Controller;
import entities.QueryResult;
import filters.IFilter;
import filters.TotalFilter;

public class MainView implements Runnable {

	private Controller conRef;
	private MetricGeneratorPanel mGen;
	private MainPanel mPanel;
	//Los vectores de filtros, qresults y Charts estan sincronizados
	private Vector<IFilter> filters;
	private Vector<QueryResult> results;
	private Vector<Chart> charts;
	
	public MainView(Controller c) {
		this.conRef = c;
	}

	@Override
	public void run() {
		mPanel = new MainPanel(this);
		mGen = new MetricGeneratorPanel(this);
		mGen.setVisible(false);
		mPanel.setVisible(true);
		filters = new Vector<IFilter>();
		results = new Vector<QueryResult>();
		charts = new Vector<Chart>();
	}

	public boolean store(Vector dataVector, String txtNroMesa, boolean selected,String barrio) {
		return conRef.store(dataVector,txtNroMesa,selected,barrio);
	}
	
	public QueryResult query(IFilter filter){
		return conRef.query(filter);
	}

	public void showMetricGen() {
		mGen.setVisible(true);		
	}

	public void addFilter(IFilter filter) {
		filters.add(filter);
		mGen.setVisible(false);
		
	}
	
	public void addChart(Chart chart){
		charts.add(chart);
	}

	public Vector<Chart> updateMetrics() {
		results.removeAllElements();
		for(IFilter filter: filters){
			results.add(query(filter));
		}		
		
		for(int i = 0; i < results.size() ; i++){
			charts.elementAt(i).updateChart(results.elementAt(i));
		}	
		return charts;
	}
}
